package com.qixin.api.serviceimpl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.qixin.api.bean.ResultPage;
import com.qixin.api.bean.TotalRole;
import com.qixin.api.bean.TotalRoleMenu;
import com.qixin.api.bean.VoPage;
import com.qixin.api.mapper.TotalRoleMapper;
import com.qixin.api.service.TotalRoleMenuService;
import com.qixin.api.service.TotalRoleService;
import com.qixin.api.service.TotalRoleUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.*;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author 李许多
 * @since 2018-11-12
 */
@Service
public class TotalRoleServiceImpl extends ServiceImpl<TotalRoleMapper, TotalRole> implements TotalRoleService {
    @Autowired
    TotalRoleMapper totalRoleMapper;
    @Autowired
    TotalRoleMenuService totalRoleMenuService;
    @Autowired
    TotalRoleUserService totalRoleUserService;
    @Override
    public ResultPage<TotalRole> selectMyPage(VoPage voPage,TotalRole totalRole) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1)*voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        List<TotalRole> list = baseMapper.selectMyPage(paramMap,totalRole);
        Integer count = baseMapper.countMyPage(paramMap,totalRole);
        ResultPage<TotalRole>resultPage=new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }
    @Override
    @Transactional(rollbackFor = {SQLException.class})
    public String deleteRole(Long id) throws SQLException {
        TotalRole totalRole=new TotalRole();
        totalRole.setId(id);
        if(this.deleteById(totalRole)){
            Map map=new HashMap<>();
            map.put("role_id",totalRole.getId());
           totalRoleUserService .deleteByMap(map);
            Map map1=new HashMap<>();
            map1.put("r_id", totalRole.getId());
            if(totalRoleMenuService.deleteByMap(map1)){
                return  "删除成功";
            }


        }
        throw   new  SQLException();
    }
    @Override
    @Transactional
    public boolean insertRoleAndMenu(String param) {
        //把传过来的json字符串转成Map集合
        Map mapObj = JSONObject.parseObject(param, Map.class);
        TotalRole role=new TotalRole();
        //获取map中 填入的角色名字
        role.setRoleName(mapObj.get("roleName")+"");
        role.setCreateTime(new Date().getTime());
        //该角色唯一标识
        role.setRoleValue(UUID.randomUUID()+"");
        //如果新增角色成功
        if(this.insert(role)){
            //获取map中所有选中的菜单id
            String menuId=(String) mapObj.get("menuId");
            //分割成独立的id
            String[] strs=menuId.split(",");
            //循环遍历数组中的所有菜单ID
            for (int i = 0; i < strs.length; i++) {
                boolean flag=false;
                TotalRoleMenu roleMenu=new TotalRoleMenu();
                roleMenu.setmId(Long.parseLong(strs[i]));
                roleMenu.setrId(role.getId());
                //保存角色和菜单关联表
                if( totalRoleMenuService.insert(roleMenu)){
                    flag=true;
                } else{
                    flag=false;
                }
                //如果flag是假  就返回false
                if (!flag){
                    return  false;
                }
            }
        }
        return  true;

    }

}
